package org.stvd.listener.workflow;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.stvd.common.workflow.WorkflowConsts;
import org.stvd.common.workflow.enums.WorkflowApproveStatusEnum;
import org.stvd.core.util.DateUtil;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.workflow.WfIndex;
import org.stvd.entities.workflow.WfLogs;
import org.stvd.service.admin.UsersService;
import org.stvd.service.workflow.ProcessCoreService;
import org.stvd.service.workflow.WfIndexService;
import org.stvd.service.workflow.WfLogsService;

/**
 * 任务被分配给了一个人员。事件包含任务
 */
public class TaskAssignedListener implements EventHandler {

	@Autowired
	WfIndexService wfIndexService;

	@Autowired
	WfLogsService wfLogsService;

	@Autowired
	UsersService usersService;
	@Autowired
	protected RepositoryService repositoryService;
	@Autowired
	private ProcessCoreService processCoreService;

	public void handle(ActivitiEvent event) {

		System.out.println("Event received: " + event.getType());

		ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
		TaskEntity taskEntity = (TaskEntity) entityEvent.getEntity();
		PvmActivity targetActivity = processCoreService.findFirstActivity(entityEvent.getProcessDefinitionId());
		

		WfIndex wfIndex = wfIndexService.findByProcInstId(taskEntity.getProcessInstanceId());
		if (wfIndex != null) {
			Boolean isfirst = false;
			if (targetActivity.getId().equals(taskEntity.getExecution().getCurrentActivityId())||targetActivity.getProperty("name").equals(wfIndex.getTaskName())) {
				isfirst = true;
			}
			wfIndex.setStatus(WorkflowApproveStatusEnum.Pending.getValue());
			wfIndex.setAssignee(taskEntity.getAssignee());
			wfIndexService.update(wfIndex);
			if (StringUtil.isEmpty(wfIndex.getHistUsers())
					&& taskEntity.getVariable(WorkflowConsts.INDEX_USER_ID).toString().equals(taskEntity.getAssignee())
					|| isfirst) {

			} else {
				String operatorName = "";
//                List<Users> users = usersService.findUsersById(Consts.GUID,taskEntity.getAssignee());
//                if(users != null && users.size() > 0){
//                    operatorName = users.get(0).getUname();
//                }
				WfLogs wfLogs = new WfLogs();
				wfLogs.setOperator(taskEntity.getAssignee());
				wfLogs.setOperatorName(operatorName);
				wfLogs.setOperateContent("");
				wfLogs.setOperateTime(DateUtil.getSystemDate());
				wfLogs.setOperateType(WorkflowConsts.HANDLER_CLAIM);
				wfLogs.setOperateName(WorkflowConsts.HANDLER_CLAIM_NAME);
				wfLogs.setProcInstId(taskEntity.getProcessInstanceId());
				wfLogs.setTaskId(taskEntity.getId());
				wfLogs.setTaskName(taskEntity.getName());
				wfLogs.setRemark("");
				wfLogsService.insert(wfLogs);
			}
		}
	}

}